/*
 * Copyright (c) 2023 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __INTELLIGENT_AGRICULTURE_H__
#define __INTELLIGENT_AGRICULTURE_H__

#include "lz_hardware.h"
#include "lcd.h"
#include "picture.h"

typedef enum
{
    WORK_MODE_AUTO,
    WORK_MODE_MANUAL,
    WORK_MODE_MAX,
} work_mode_e;

/***************************************************************
* 名   称: SWITCH_STATUS_E
* 说    明: 开关状态枚举类型
***************************************************************/
typedef enum
{
    OFF = 0,
    ON
} SWITCH_STATUS_E;

/***************************************************************
* 名   称: GRF_SOC_CON29
* 说    明: GRF_SOC_CON29寄存器结构体
***************************************************************/
typedef union
{
    uint32_t value;
    struct
    {
        uint16_t grf_saradc_ana_reg_low:  4;
        uint16_t grf_saradc_vol_sel:      1;
        uint16_t grf_saradc_ana_reg_high: 11;
        struct
        {
            uint16_t grf_saradc_ana_reg_low:  4;
            uint16_t grf_saradc_vol_sel:      1;
            uint16_t grf_saradc_ana_reg_high: 11;
        } rw;
    };
} GRF_SOC_CON29;

/***************************************************************
* 名    称: ia_adc_dev_t
* 说    明: ADC设备结构体
***************************************************************/
typedef struct
{
    int                        init;               //初始化状态
    int                        port;               //端口
    uint32_t                   value;              //adc值
    Pinctrl                    adc;                //io总线
} ia_adc_dev_t;

/***************************************************************
* 名    称: ia_i2c_dev_t
* 说    明: I2C设备结构体
***************************************************************/
typedef struct
{
    int                        init;               //初始化状态
    int                        port;               //端口
    int                        slave_addr;         //从地址
    int                        speed;              //速度
    I2cBusIo                   i2c;                //io总线
} ia_i2c_dev_t;

/***************************************************************
* 名   称:ia_relay_dev_t
* 说    明: 继电器设备结构体
***************************************************************/
typedef struct
{
    int                        init;               //初始化状态
    int                        onoff;              //开关状态
    Pinctrl                    relay;              //io总线
} ia_relay_dev_t;

/***************************************************************
* 名   称: ia_pwm_dev_t
* 说    明: PWM设备结构体
***************************************************************/
typedef struct
{
    int                        init;               //初始化状态
    int                        port;               //端口
    uint32_t                   cycle;              //周期
    uint8_t                    onoff;              //开关
    PwmBusIo                   pwmio;              //io总线
} ia_pwm_dev_t;

/***************************************************************
* 名   称: ia_lcd_param_t
* 说    明: lcd参数结构体
***************************************************************/
typedef struct
{
    uint16_t                   w;
    uint16_t                   h;
    uint8_t                    dir;                //显示方向 设置横屏或者竖屏显示 0或1为竖屏 2或3为横屏
    uint16_t                   f_color;            //字体颜色
    uint16_t                   b_color;            //背景颜色
    uint16_t                   f_size;             //字体尺寸
    uint8_t                    start_line;         //显示内容起始行
} ia_lcd_param_t;


/***************************************************************
* 名   称: ia_soil_humidity_t
* 说    明: 土壤湿度检测结构体
***************************************************************/
typedef struct
{
    int                        is_continue;        //是否继续 1继续 0暂停
    int                        voltage;            //模拟电压值mv
    float                      value;              //土壤湿度值
    ia_adc_dev_t               adc_dev;            //ADC设备
} ia_soil_humidity_t;

/***************************************************************
* 名   称: ia_light_intensity_t
* 说    明: 光强检测结构体
***************************************************************/
typedef struct
{
    int                        is_continue;        //是否继续 1继续 0暂停
    float                      value;              //光强值
    ia_i2c_dev_t               i2c_dev;            //I2C设备
} ia_light_intensity_t;

/***************************************************************
* 名   称: ia_water_pump_t
* 说    明: 水泵控制结构体
***************************************************************/
typedef struct
{
    int                        onoff;              //开关状态
    ia_relay_dev_t             relay_dev;          //继电器设备
} ia_water_pump_t;

/***************************************************************
* 名   称: ia_shed_cover_t
* 说    明: 棚罩控制结构体
***************************************************************/
typedef struct
{
    int                        onoff;              //棚罩开关状态
    uint32_t                   on_duty;            //开占空比
    uint32_t                   off_duty;           //关占空比
    ia_pwm_dev_t               pwm_dev;            //PWM设备
} ia_shed_cover_t;


/***************************************************************
* 名   称: ia_lcd_t
* 说    明: lcd结构体
***************************************************************/
typedef struct
{
    ia_lcd_param_t             param;          //lcd参数
} ia_lcd_t;

/***************************************************************
* 名   称: ia_dev_t
* 说    明: 智慧农业结构体
***************************************************************/
typedef struct
{
    ia_soil_humidity_t         *soil_humidity;
    ia_light_intensity_t       *light_intensity;
    ia_water_pump_t            *water_pump;
    ia_shed_cover_t            *shed_cover;
    ia_lcd_t                   *lcd;
} ia_dev_t;

/***************************************************************
* 名   称: thread_id_e
* 说    明: 线程ID枚举类型
***************************************************************/
typedef enum
{
    THREAD_ID_SOIL_HUMIDITY,
    THREAD_ID_LIGHT_INTENSITY,
    THREAD_ID_WATER_PUMP,
    THREAD_ID_SHED_COVER,
    THREAD_ID_LCD,
    THREAD_ID_IOT,
    THREAD_ID_MAX,
} thread_id_e;

/***************************************************************
* 名   称: display_data_t
* 说    明: 显示数据结构体
***************************************************************/
typedef struct
{
    float           soil_humidity;
    float           light_intensity;
    int             water_pump_onoff: 1;
    int             shed_cover_onoff: 1;
} display_data_t;

typedef uint32_t athread_id[THREAD_ID_MAX];

/***************************************************************
* 名   称: ia_example_t
* 说    明: 智慧农业程结构体
***************************************************************/
typedef struct
{
    ia_dev_t        *dev;
    athread_id      thread_id;
    display_data_t  display_data;
    display_data_t  display_data_last;
    work_mode_e work_mode;
} ia_example_t;

void ia_soil_humidity_init(ia_soil_humidity_t *soil_humidity);
void ia_light_intensity_init(ia_light_intensity_t *light_intensity);
void ia_water_pump_init(ia_water_pump_t *water_pump);
void ia_shed_cover_init(ia_shed_cover_t *shed_cover);
void ia_lcd_init(ia_lcd_param_t *lcd_param);

float ia_get_soil_humidity(ia_soil_humidity_t *soil_humidity);
float ia_get_light_intensity(ia_light_intensity_t *light_intensity);
void ia_set_water_pump(ia_water_pump_t *water_pump, SWITCH_STATUS_E status);
void ia_set_shed_cover(ia_shed_cover_t *shed_cover, SWITCH_STATUS_E status);

void ia_lcd_soil_humidity_display(ia_lcd_t *ia_lcd, ia_dev_t *ia_dev);
void ia_lcd_water_pump_display(ia_lcd_t *ia_lcd, ia_dev_t *ia_dev);
void ia_lcd_light_intensity_display(ia_lcd_t *ia_lcd, ia_dev_t *ia_dev);
void ia_lcd_shed_cover_display(ia_lcd_t *ia_lcd, ia_dev_t *ia_dev);
void ia_lcd_data_display(ia_lcd_t *ia_lcd, ia_dev_t *ia_dev);

extern ia_dev_t g_ia_dev;
extern ia_example_t g_ia_example;

#endif // __INTELLIGENT_AGRICULTURE_H__